<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_controller extends CI_Controller {

	function __construct()
	{
		parent::__construct();

		$this->load->model('user_model');
	}

	/**
	* Show packages list
	*/
	public function index()
	{
		$this->load->view('public/user/sign_in');
	}

	/**
	 * Show sign in page
	 */
	public function sign_in() 
	{
		$this->load->view('public/user/sign_in');
	}

	public function perform_signin()
	{
		if($this->input->post())
		{
			$user_exists = $this->user_model->check_credentials($this->input->post('username'), $this->input->post('password'));

			if($user_exists == false)
			{
				$this->session->set_flashdata('error', 'Invalid username and password');
				redirect('user/sign_in');
			}

			$this->session->set_userdata('login_id',  $this->user_model->id);
			$this->session->set_userdata('role', $this->user_model->role);
			$this->session->set_userdata('role_id', $this->user_model->role_id);
			$this->session->set_userdata('firstname', $this->user_model->firstname);
			$this->session->set_userdata('lastname', $this->user_model->lastname);

			redirect('');
		}
	}

	/**
	 *	Show registration page 
	 */
	public function register() 
	{
		$this->load->view('public/user/register');
	}

	public function perform_register() {
		if($this->input->post()) {
			$add = $this->user_model->add_by_user_post($this->input->post());

			if($add) 
			{
				//redirect('');
			}

			$this->register();
		}
	}

	public function sign_out()
	{
		$this->session->sess_destroy();
		redirect('user/sign_in');
	}

}